package com.polaris.live.widget.task

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.databinding.WidgetAnchorTaskProgressBinding
import com.polaris.live.dialog.common.TipsDialog
import com.polaris.live.resp.back_resp.UserTask
import com.polaris.live.utils.image.ImageUtils

/**
 * AnchorTaskProgressView 主播任务带进度
 * @author Created by 天晴 on 2024/1/24 19:17
 * @since 1.0.0
 **/
class AnchorTaskProgressView(context: Context, attributeSet: AttributeSet) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WidgetAnchorTaskProgressBinding.inflate(LayoutInflater.from(context), this)
    private var clickBack: ((explainType: Long) -> Unit)? = null
    private var goBack: ((reward: UserTask) -> Unit)? = null

    fun setItemListener(
        clickBack: ((explainType: Long) -> Unit)? = null,
        goBack: ((reward: UserTask) -> Unit)? = null,
    ) {
        this.clickBack = clickBack
        this.goBack = goBack
    }

    private fun initListener(item: UserTask) {
        mBinding.question.setOnClickListener {
            TipsDialog.show(mBinding.question.context, item.name, item.explain)
        }
        mBinding.tpView.setItemListener {
            clickBack?.invoke(it)
        }
        mBinding.question.setOnClickListener {
            TipsDialog.show(context, item.name, item.explain)
        }
        mBinding.tvStatus.setOnClickListener {
            goBack?.invoke(item)
        }
        mBinding.rewardLl.setOnClickListener {
            clickBack?.invoke(item.rewardExplainType)
        }
    }

    fun setData(item: UserTask) {
        initListener(item)
        mBinding.apply {
            Glide.with(context)
                .load(ImageUtils.imgPath(item.photoUrl))
                .into(titleImg)
            titleTv.text = item.name

            Glide.with(img.context)
                .load(ImageUtils.imgPath(item.rewardIconUrl))
                .into(img)
            tvNum.text = "x${item.rewardAmount}"

            mBinding.question.visibility = if (item.explain.isEmpty()) View.GONE else View.VISIBLE
            mBinding.tpView.setData(item.rewards)

            val (statusText, textColor, backgroundResource) = when {
                item.rewards.any { it.status == 1L } -> {
                    Triple(
                        StringUtils.getString(com.polaris.live.translation.R.string.lucky_bag_bar_claimable),
                        ColorUtils.getColor(R.color.white),
                        R.drawable.shape_task_bg
                    )
                }
                item.rewards.any { it.status == 0L } -> {
                    Triple(
                        StringUtils.getString(com.polaris.live.translation.R.string.go_to_finish),
                        ColorUtils.getColor(R.color.color_893AFF),
                        R.drawable.r20_shape_893aff_stork
                    )
                }
                else -> {
                    Triple(
                        StringUtils.getString(com.polaris.live.translation.R.string.task_completed),
                        ColorUtils.getColor(R.color.color_000000_30),
                        R.drawable.r20_shape_000000_30_stork
                    )
                }
            }
            tvStatus.text = statusText
            tvStatus.setTextColor(textColor)
            tvStatus.setBackgroundResource(backgroundResource)

        }
    }

}